Microsoft® Visual Basic® Scripting Edition VBScript and Forms |
| VBScript Tutorial | | Previous | Next |
|
You can use Visual Basic Scripting Edition to do much of the form processing that you'd usually have to do on a server. You can also do things that just can't be done on the server.Here's an example of simple client-side validation. The HTML code is for a text box and a button. If you use Microsoft Internet Explorer version 3.0 to view the page produced by the code, you'll see a small text box with a button next to it.
The only difference between this text box and the examples on A Simple VBScript Page is that the Value property of the text box is used to check the entered value. To get the Value property, the code has to qualify the reference to the name of the text box.<HTML> <HEAD><TITLE>Simple Validation</TITLE> <SCRIPT LANGUAGE="VBScript"> <!-- Sub Submit_OnClick Dim TheForm Set TheForm = Document.ValidForm If IsNumeric(TheForm.Text1.Value) Then If TheForm.Text1.Value < 1 Or TheForm.Text1.Value > 10 Then MsgBox "Please enter a number between 1 and 10." Else MsgBox "Thank you." End If Else MsgBox "Please enter a numeric value." End If End Sub --> </SCRIPT> </HEAD> <BODY> <H3>Simple Validation</H3><HR> <FORM NAME="ValidForm"> Enter a value between 1 and 10: <INPUT NAME="Text1" TYPE="TEXT" SIZE="2"> <INPUT NAME="Submit" TYPE="BUTTON" VALUE="Submit"> </FORM> </BODY> </HTML>You can always write out the full reference Document.ValidForm.Text1. However, where you have multiple references to form controls, you'll want to do what was done here. First declare a variable. Then use the Set statement to assign the form to the variable TheForm. A regular assignment statement doesn't work here; you must use Set to preserve the reference to an object.
Notice that the example directly tests the value against a number: it uses the IsNumeric function to make sure the string in the text box is a number. Although VBScript automatically converts strings and numbers, it's always a good practice to test a user-entered value for its data subtype and to use conversion functions as necessary. In addition, when doing summation with text box values, convert the values explicitly to numbers since the plus sign (+ operator) represents both addition and string concatenation. For example, if Text1 contains "1" and Text2 contains "2", you see the following results:A = Text1.Value + Text2.Value ' A is "12" A = CDbl(Text1.Value) + Text2.Value ' A is 3
The simple validation example uses a plain button control. If it used a Submit control, the example would never see the data to check it—everything would go immediately to the server. Avoiding the Submit control lets you check the data, but it doesn't submit it to the server. That requires an additional line of code:To send the data to the server, the code invokes the Submit method on the form object when the data is correct. From there, the server handles the data just as it otherwise would—except that the data is correct before it gets there. You'll find complete information about the Submit method and other methods on the Internet Explorer Scripting Object Model page.<SCRIPT LANGUAGE="VBScript"> <!-- Sub Submit_OnClick Dim TheForm Set TheForm = Document.ValidForm If IsNumeric(TheForm.Text1.Value) Then If TheForm.Text1.Value < 1 Or TheForm.Text1.Value > 10 Then MsgBox "Please enter a number between 1 and 10." Else MsgBox "Thank you." TheForm.Submit ' Data correct; send to server. End If Else MsgBox "Please enter a numeric value." End If End Sub --> </SCRIPT>So far, you've seen only the standard HTML <FORM> objects. Internet Explorer 3.0 also lets you exploit the full power of ActiveX™ controls (formerly called OLE controls) and Java objects.